/**
 * 获取植物信息
 * */
const {
    express,
    db_mysql,
    errorData,
    strLen
} = require('./common-req.js')
const router = express.Router();
const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf8');
/**
 * 根据门、纲、科、属获取植物信息
 * */
router.get('/', (req, res) => {
    const doorId = req.query.doorId;
    const outlineId = req.query.outlineId;
    const sectionId = req.query.sectionId;
    const genusId = req.query.genusId;
    let doorIdSqlStr = '';
    let outlineIdSqlStr = '';
    let sectionIdSqlStr = '';
    let genusIdSqlStr = '';
    let countSqlArr = [];
    let sqlArr = [];
    let paramsCount = 0;
    if((typeof doorId) === 'string') {
        if(paramsCount > 0) {
            doorIdSqlStr = ' and doorId=?';
        } else {
            doorIdSqlStr = ' doorId=?';
        }
        countSqlArr.push(doorId);
        sqlArr.push(doorId);
        paramsCount++;
    }
    if((typeof outlineId) === 'string') {
        if(paramsCount > 0) {
            outlineIdSqlStr = ' and outlineId=?';
        } else {
            outlineIdSqlStr = ' outlineId=?';
        }
        countSqlArr.push(outlineId);
        sqlArr.push(outlineId);
        paramsCount++;
    }
    if((typeof sectionId) === 'string') {
        if(paramsCount > 0) {
            sectionIdSqlStr = ' and sectionId=?';
        } else {
            sectionIdSqlStr = ' sectionId=?';
        }
        countSqlArr.push(sectionId);
        sqlArr.push(sectionId);
        paramsCount++;
    }
    if((typeof genusId) === 'string') {
        if(paramsCount > 0) {
            genusIdSqlStr = ' and genusId=?';
        } else {
            genusIdSqlStr = ' genusId=?';
        }
        countSqlArr.push(genusId);
        sqlArr.push(genusId);
        paramsCount++;
    }
    let whereStr = '';
    if(doorIdSqlStr != '' || outlineIdSqlStr != '' || sectionIdSqlStr != '' || genusIdSqlStr != '') {
        whereStr = ' where';
    } else {
        whereStr = '';
    }
    let pageNum = parseInt(req.query.pageNum) - 1 > 0 ? parseInt(req.query.pageNum) - 1 : 0;
    let pageSize = parseInt(req.query.pageSize);
    sqlArr.push(pageNum*pageSize, pageSize);
    if(isNaN(pageNum) || isNaN(pageSize)) {
        res.send('存在NaN')
        return;
    }
    let countSql = 'select count(*) as count from botanys' + whereStr + doorIdSqlStr + outlineIdSqlStr + sectionIdSqlStr + genusIdSqlStr;
    let doorSql = 'select * from botanys' + whereStr + doorIdSqlStr + outlineIdSqlStr + sectionIdSqlStr + genusIdSqlStr + ' limit ?,?';

    let resData = {
        data: [],
        status: 0,
        msg: '获取植物数据成功',
        total: 0
    }
    db_mysql.select(countSql, countSqlArr, countRes => {
        resData.total = countRes[0].count;
        db_mysql.select(doorSql, sqlArr, results => {
            if (results && results.length > 0) {
                for(var k in results) {
                    let base = decoder.write(results[k].baseInfo);
                    let grow = decoder.write(results[k].growInfo);
                    let form = decoder.write(results[k].formInfo);
                    let cul = decoder.write(results[k].cultivateInfo);
                    var temp = {
                        botanyId: results[k].botanyId,
                        doorId: results[k].doorId,
                        outlineId: results[k].outlineId,
                        sectionId: results[k].sectionId,
                        genusId: results[k].genusId,
                        name: results[k].name,
                        baseInfo: base === '' ? base : JSON.parse(base),
                        growInfo: grow === '' ? grow : JSON.parse(grow),
                        formInfo: form === '' ? form : JSON.parse(form),
                        cultivateInfo: cul === '' ? cul : JSON.parse(cul),
                        sectionId: results[k].sectionId,
                        src: results[k].src,
                        time: results[k].time
                    }
                    resData.data.push(temp);
                }
                res.send(resData);
            } else {
                resData.msg = '无数据';
                res.send(resData);
            }
        })
    })
})
/**
 * 根据植物编号获取植物信息
 * */
router.get('/:botanyId', (req, res) => {
    const botanyId = req.params.botanyId;
    let botanySql = 'select * from botanys where botanyId=?;'
    db_mysql.select(botanySql, botanyId, results => {
        if (results && results.length > 0) {
            let resData = {
                data: {},
                status: 0,
                msg: '获取植物数据成功'
            }
            let base = decoder.write(results[0].baseInfo);
            let grow = decoder.write(results[0].growInfo);
            let form = decoder.write(results[0].formInfo);
            let cul = decoder.write(results[0].cultivateInfo);
            resData.data = {
                botanyId: results[0].botanyId,
                doorId: results[0].doorId,
                outlineId: results[0].outlineId,
                sectionId: results[0].sectionId,
                genusId: results[0].genusId,
                name: results[0].name,
                baseInfo: base === '' ? base : JSON.parse(base),
                growInfo: grow === '' ? grow : JSON.parse(grow),
                formInfo: form === '' ? form : JSON.parse(form),
                cultivateInfo: cul === '' ? cul : JSON.parse(cul),
                sectionId: results[0].sectionId,
                src: results[0].src,
                time: results[0].time,
            }
            res.send(resData);
        } else {
            errorData.msg = '无该数据';
            res.send(errorData);
        }
    }) 
})

router.get('/random/botanys', (req, res) => {
    let startNum = parseInt(Math.random() * 2);
    const pageSize = parseInt(req.query.pageSize);
    let botanySql = 'select * from botanys limit ?,?';
    let botanyArr = [startNum, pageSize];
    let countSql = 'select count(*) as total from botanys';
    db_mysql.select(countSql, null, result => {
        if (result && result.length > 0) {
            let resData = {
                data: [],
                status: 0,
                msg: '获取植物数据成功',
                total: result[0].total
            }
            db_mysql.select(botanySql, botanyArr, results => {
                if (results && results.length > 0) {
                    for(var k in results) {
                        let base = decoder.write(results[k].baseInfo);
                        let grow = decoder.write(results[k].growInfo);
                        let form = decoder.write(results[k].formInfo);
                        let cul = decoder.write(results[k].cultivateInfo);
                        var temp = {
                            botanyId: results[k].botanyId,
                            doorId: results[k].doorId,
                            outlineId: results[k].outlineId,
                            sectionId: results[k].sectionId,
                            genusId: results[k].genusId,
                            name: results[k].name,
                            baseInfo: base === '' ? base : JSON.parse(base),
                            growInfo: grow === '' ? grow : JSON.parse(grow),
                            formInfo: form === '' ? form : JSON.parse(form),
                            cultivateInfo: cul === '' ? cul : JSON.parse(cul),
                            sectionId: results[k].sectionId,
                            src: results[k].src,
                            time: results[k].time
                        }
                        resData.data.push(temp);
                    }
                    res.send(resData);
                } else {
                    errorData.msg = '无数 据数据';
                    res.send(errorData);
                }
            })
        } else {
            errorData.msg = '暂无数据数据';
            res.send(errorData);
        }
    })
})

/**
 * 获取全部植物信息
 * */
router.get('/all/botanys', (req, res) => {
    const pageNum = parseInt(req.query.pageNum) - 1 >= 0 ? parseInt(req.query.pageNum) - 1 : 0;
    const pageSize = parseInt(req.query.pageSize);
    let botanySql = 'select * from botanys limit ?,?';
    let botanyArr = [pageNum * pageSize, pageSize];
    let countSql = 'select count(*) as total from botanys';
    db_mysql.select(countSql, null, result => {
        if (result && result.length > 0) {
            let resData = {
                data: [],
                status: 0,
                msg: '获取植物数据成功',
                total: result[0].total
            }
            db_mysql.select(botanySql, botanyArr, results => {
                if (results && results.length > 0) {
                    for(var k in results) {
                        let base = decoder.write(results[k].baseInfo);
                        let grow = decoder.write(results[k].growInfo);
                        let form = decoder.write(results[k].formInfo);
                        let cul = decoder.write(results[k].cultivateInfo);
                        var temp = {
                            botanyId: results[k].botanyId,
                            doorId: results[k].doorId,
                            outlineId: results[k].outlineId,
                            sectionId: results[k].sectionId,
                            genusId: results[k].genusId,
                            name: results[k].name,
                            baseInfo: base === '' ? base : JSON.parse(base),
                            growInfo: grow === '' ? grow : JSON.parse(grow),
                            formInfo: form === '' ? form : JSON.parse(form),
                            cultivateInfo: cul === '' ? cul : JSON.parse(cul),
                            sectionId: results[k].sectionId,
                            src: results[k].src,
                            time: results[k].time
                        }
                        resData.data.push(temp);
                    }
                    res.send(resData);
                } else {
                    errorData.msg = '暂无数据数据';
                    res.send(errorData);
                }
            })
        } else {
            errorData.msg = '暂无数据数据';
            res.send(errorData);
        }
    }) 
})

// 根据关键字获取植物名称
router.get('/tip/:keyword', (req, res) => {
    const keyword = `%${req.params.keyword}%` 
    let selSql = 'select name from botanys where name like ? limit 0, 8';
    let selArr = [keyword];
    let resData = {
        data: [],
        msg: '查询成功',
        status: 0,
        code: 200
    }
    db_mysql.select(selSql, selArr, results => {
        if(results && results.length > 0) {
            results.forEach(item => {
                var obj = {
                    name: item.name
                }
                resData.data.push(obj)
            })
        }
        res.send(resData);
    })
})
module.exports = router;
